export default {
    namespaced: true,
    state: {
        loggedIn: false,
        //用户类型
        userType: 0,
        //路由
        roles: [],
        //权限
        permission: 0,
        name: ""
    },
    mutations: {
        updateLoggedIn(state, val = true) {
            state.loggedIn = val
        },
        updateUserType(state, val) {
            state.userType = val
        },
        updateRoles(state, val) {
            state.roles = val
        },
        updatePermission(state, val) {
            state.permission = val
        },
        updateName(state, val) {
            state.name = val
        }
    },
    actions: {
        //异步修改路由并用Promise返回
        asyncRoles(store, val) {
            return new Promise((resolve) => {
                let group = []
                for (let bit = 0; bit < 8; bit++) {
                    const power = 1 << bit;
                    if ((power & store.state.permission) === power) {
                        // console.log(power);
                        group.push(power);
                    }
                }
                let newRoles = val.filter(item => {
                    return group.includes(item.children[0].meta.role)
                })
                store.commit("updateRoles", newRoles)
                resolve(newRoles)
            })
        }
    }
}